package com.kaesar.leetcode.LeetCode_051_100;

public class LeetCode_063 {
    /**
     * 递归法
     *
     * @param obstacleGrid
     * @return
     */
    public static int uniquePathsWithObstacles(int[][] obstacleGrid) {
        int m = obstacleGrid.length, n = obstacleGrid[0].length;
        if (m == 1 && n == 1) {
            if (obstacleGrid[m - 1][n - 1] == 1) {
                return 0;
            } else {
                return 1;
            }
        }
        if (obstacleGrid[m - 1][n - 1] == 1) {
            return 0;
        }
        return uniquePathsWithObstacles(obstacleGrid, obstacleGrid.length - 1, obstacleGrid[0].length - 1);
    }

    private static int uniquePathsWithObstacles(int[][] obstacleGrid, int x, int y) {
        if (obstacleGrid[x][y] == 1) {
            return 0;
        }
        if (x == 0 && y == 0) {
            if (obstacleGrid[x][y] == 1) {
                return 0;
            } else {
                return 1;
            }
        }

        if (x > 0) {
            if (y > 0) {
                if (obstacleGrid[x - 1][y] == 1) {
                    if (obstacleGrid[x][y - 1] == 1) {
                        return 0;
                    } else {
                        return uniquePathsWithObstacles(obstacleGrid, x, y - 1);
                    }
                } else {
                    if (obstacleGrid[x][y - 1] == 1) {
                        return uniquePathsWithObstacles(obstacleGrid, x - 1, y);
                    } else {
                        return uniquePathsWithObstacles(obstacleGrid, x - 1, y) + uniquePathsWithObstacles(obstacleGrid, x, y - 1);
                    }
                }
            } else {
                if (obstacleGrid[x - 1][y] == 1) {
                    return 0;
                } else {
                    return uniquePathsWithObstacles(obstacleGrid, x - 1, y);
                }
            }
        } else {
            if (obstacleGrid[x][y - 1] == 1) {
                return 0;
            } else {
                return uniquePathsWithObstacles(obstacleGrid, x, y - 1);
            }

        }
    }

    /**
     * 迭代法
     *
     * @param obstacleGrid
     * @return
     */
    public static int uniquePathsWithObstacles2(int[][] obstacleGrid) {
        int m = obstacleGrid.length, n = obstacleGrid[0].length;
        int[] columns = new int[n];
        if (obstacleGrid[0][0] == 1) {
            columns[0] = 0;
        } else {
            columns[0] = 1;
        }
        // 初始化第一行
        for (int i = 1; i < n; i++) {
            if (columns[i - 1] == 0) {
                columns[i] = 0;
                continue;
            }
            if (obstacleGrid[0][i] == 1) {
                columns[i] = 0;
            } else {
                columns[i] = 1;
            }
        }

        // 迭代过程
        for (int i = 1; i < m; i++) {
            // 第一个值
            if (obstacleGrid[i][0] == 1) {
                columns[0] = 0;
            } else {
                columns[0] = 1;
            }
            for (int j = 1; j < n; j++) {
                if (obstacleGrid[i][j] == 1) {
                    columns[j] = 0;
                } else {
                    columns[j] = columns[j] + columns[j - 1];
                }
            }
        }

        return columns[n - 1];
    }

    public static void main(String[] args) {
        int[][] obstacleGrid = new int[][]{{0}, {1}};
        System.out.println(uniquePathsWithObstacles(obstacleGrid));
        System.out.println(uniquePathsWithObstacles2(obstacleGrid));
    }
}
